package com.example.demo.controller;

import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.*;
import java.util.*;
import java.util.List;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/student/list")
    public String list(ModelMap map ,Student student){
        if ("".equals(student.getName())){
            student.setName(null);
        }
        map.put("students",studentService.findAll(student));
        return "student/list";
    }
    @RequestMapping("/student/delete/{id}")
    public String delete(@PathVariable String id){
//        System.out.println("id:"+id);
        studentService.deleteById(id);
        return "redirect:/student/list" ;
    }
    @RequestMapping("/student/create")
    public String create(){
        return "student/create";
    }
    @RequestMapping("/student/save")
    public String save(Student s){
//        System.out.println(s.getName());
        if (s.getId()==null){
            s.setId(UUID.randomUUID().toString());
        }
        studentService.save(s);
        return "redirect:/student/list";
    }
    @RequestMapping("/student/update/{id}")
    public String update(@PathVariable String id,ModelMap map){
        Student s=studentService.getReferenceById(id);
        map.put("student",s);
        return "student/update";
    }

}
